package com.wuzhun.wxmp.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.wuzhun.wxmp.mapper.MessageMapper;
import com.wuzhun.wxmp.model.dto.MessageDTO;
import com.wuzhun.wxmp.model.dto.WechatDTO;
import com.wuzhun.wxmp.model.entity.Message;
import com.wuzhun.wxmp.model.entity.Wechat;
import com.wuzhun.wxmp.service.IMessageService;
import com.wuzhun.wxmp.service.IWechatService;
import com.wuzhun.wxmp.util.DateTimeUtil;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * <p>
 * 文章表 服务实现类
 * </p>
 *
 * @author Paulin
 * @since 2023-05-31
 */
@Service
public class MessageServiceImpl extends ServiceImpl<MessageMapper, Message> implements IMessageService {
    @Resource
    private MessageMapper messageMapper;

    @Resource
    private IWechatService wechatService;
    /**
     * 简介长度
     */
    public static final int CONTENT_DESC_LENGTH = 50;


    @Override
    public Page<MessageDTO> listByPage(Page<Message> pageMessage, QueryWrapper<Message> queryWrapper) {
        Page<Message> messagePage = messageMapper.selectPage(pageMessage, queryWrapper);
        return covertPageMessage(messagePage);
    }

    @Override
    public MessageDTO queryOne(Integer id) {
        Message message = messageMapper.selectById(id);
        if (message == null) {
            return null;
        }
        return convertMessage(message);
    }

    @Override
    public List<MessageDTO> queryListWrapper(QueryWrapper<Message> queryWrapper) {
        List<Message> messageList = messageMapper.selectList(queryWrapper);
        if (CollectionUtils.isEmpty(messageList)) {
            return Collections.emptyList();
        }
        List<MessageDTO> messageDTOList = Lists.newArrayListWithCapacity(messageList.size());
        messageList.forEach(message -> {
            messageDTOList.add(convertMessage(message));
        });
        return messageDTOList;
    }

    private Page<MessageDTO> covertPageMessage(Page<Message> pageMessage) {

        Page<MessageDTO> pageMessageDTO = new Page<>();
        if (pageMessage == null || CollectionUtils.isEmpty(pageMessage.getRecords())) {
            return pageMessageDTO;
        }
        pageMessageDTO.setCurrent(pageMessage.getCurrent());
        pageMessageDTO.setTotal(pageMessage.getTotal());
        pageMessageDTO.setPages(pageMessage.getPages());
        pageMessageDTO.setSize(pageMessage.getSize());
        List<Message> messageList = pageMessage.getRecords();
        List<MessageDTO> messageDTOList = Lists.newArrayListWithCapacity(messageList.size());
        messageList.forEach(message -> messageDTOList.add(convertMessage(message)));
        fillWechatInfo(messageDTOList);
        pageMessageDTO.setRecords(messageDTOList);
        return pageMessageDTO;
    }

    private MessageDTO convertMessage(Message message) {
        MessageDTO messageDTO = new MessageDTO();
        BeanUtils.copyProperties(message, messageDTO);
        if(StringUtils.isNotEmpty(message.getContent())) {
            String contentDesc = StringUtils.substring(message.getContent(), 0, Math.min(CONTENT_DESC_LENGTH, message.getContent().length()));
            if (message.getContent().length() > CONTENT_DESC_LENGTH) {
                contentDesc += "...";
            }
            messageDTO.setContentDesc(contentDesc);
        }
        messageDTO.setFormatCreatedTime(DateTimeUtil.getFormatDateTime(message.getCreatedTime()));
        return messageDTO;
    }

    /**
     * 补充分类信息
     *
     * @param messageDTOList
     * @return
     */
    private void fillWechatInfo(List<MessageDTO> messageDTOList) {
        List<String> wechatIdList = messageDTOList.stream().map(MessageDTO::getMpId).collect(Collectors.toList());
        List<WechatDTO> wechatDTOList = wechatService.queryListByWrapper(new QueryWrapper<Wechat>().in("mp_id", wechatIdList));
        if (CollectionUtils.isNotEmpty(wechatDTOList)) {
            Map<String, WechatDTO> wechatMap = wechatDTOList.stream().collect(Collectors.toMap(WechatDTO::getMpId, Function.identity()));
            messageDTOList.forEach(messageDTO -> {
                WechatDTO wechatDTO = wechatMap.get(messageDTO.getMpId());
                if (wechatDTO != null) {
                    messageDTO.setMpName(wechatDTO.getName());
                    messageDTO.setDepartment(wechatDTO.getDepartment());
                }
            });
        }
    }
}